<template>
	<view class="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in compList" :key="item.attr.compId" class="f-item b-b"
				:class="{active: item.attr.compId === currentId}" @click="tabtap(item.attr.compId)">
				<view class="title">{{item.attr.title[0]}}</view>
				<view class="sub-title">{{item.attr.title[1]}}</view>
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" :scroll-into-view="scrollIntoView">
			<view v-for="item in compList" :key="item.attr.compId" class="s-list" :id="'main-'+item.attr.compId">
				<text class="s-item">{{item.attr.title[0]}} {{item.attr.title[1]}}</text>
				<Draggable class="t-list" v-model="item.child" item-key="attr.compId"
					:group="{ name: 'component', pull: 'clone', put: false }" :sort="false">
					<template v-for="(sv,sk) in item.child">
						<component class="t-item"  :is="sv.tag" v-bind="sv.attr" v-on="sv.event">
							{{typeof sv.child !='undefined' ?sv.child[0].text:''}}
							<component v-if="sv.child" v-for="(tv,tk) in sv.child" :key="tk" :is="tv.tag" v-bind="tv.attr"
								v-on="tv.event">
								{{typeof tv.child !='undefined' ?tv.child[0].text:''}}
							</component>
						</component>
					</template>
				</Draggable>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		html2json,
		json2html
	} from 'html2json'
	import EButton from './components/EButton'
	import Element from 'element-ui'
	import Antd from 'ant-design-vue'
	import Draggable from "vuedraggable";
	import {
		nanoid
	} from "nanoid"
	export default {
		components: {
			Draggable
		},
		data() {
			return {
				scrollIntoView: '',
				currentId: '',
				compList: []
			}
		},
		created() {
			this.init()
		},
		methods: {
			init() { 
				// switch(opt){
				// 	case 'Element':
				// 	break
				// 	case 'Antd':
				// 	breakß
				// 	case 'Uview':
				// 	break
				// }
				// let html = '<title title="按钮 Button">'
				// html +=
				// 	'<el-button>Default</el-button><el-button type="primary">Primary</el-button><el-button type="success">Success</el-button><el-button type="info">Info</el-button><el-button type="warning">Warning</el-button><el-button type="danger">Danger</el-button>'
				// html +=
				// 	'<el-button plain>Plain</el-button><el-button type="primary" plain>Primary</el-button><el-button type="success" plain>Success</el-button><el-button type="info" plain>Info</el-button><el-button type="warning" plain>Warning</el-button><el-button type="danger" plain>Danger</el-button>'
				// html +=
				// 	'<el-button round>Round</el-button><el-button type="primary" round>Primary</el-button><el-button type="success" round>Success</el-button><el-button type="info" round>Info</el-button><el-button type="warning" round>Warning</el-button><el-button type="danger" round>Danger</el-button>'
				// html +=
				// 	'<el-button icon="el-icon-search" circle></el-button><el-button type="primary" icon="el-icon-edit" circle></el-button><el-button type="success" icon="el-icon-check" circle></el-button><el-button type="info" icon="el-icon-message" circle></el-button><el-button type="warning" icon="el-icon-star-off" circle></el-button><el-button type="danger" icon="el-icon-delete" circle></el-button>'
				// html +=
				// 	'<el-button type="primary" icon="el-icon-edit"></el-button><el-button type="primary" icon="el-icon-share"></el-button><el-button type="primary" icon="el-icon-delete"></el-button><el-button type="primary" icon="el-icon-search">搜索</el-button><el-button type="primary">上传<i class="el-icon-upload el-icon-right"></i></el-button>'
				// html +=
				// 	'<el-button type="primary" icon="el-icon-arrow-left" >上一页</el-button><el-button type="primary">下一页<i class="el-icon-arrow-right el-icon--right"></i></el-button>'
				// html +=
				// 	'<el-button type="primary" icon="el-icon-edit"></el-button><el-button type="primary" icon="el-icon-share"></el-button><el-button type="primary" icon="el-icon-delete"></el-button>'
				// html += '<el-button type="primary" :loading="true">Loading</el-button>'
				// html +=
				// 	'<el-button size="large" round>Large</el-button><el-button round>Default</el-button><el-button size="small" round>Small</el-button><el-button size="large" :icon="Search" round>Search</el-button><el-button :icon="Search" round>Search</el-button><el-button size="small" :icon="Search" round>Search</el-button>'
				// html += '</title>'
				// let json = html2json(html)
				// json.child.map((fv, fk) => {
				// 	fv.attr.compId = nanoid()
				// 	if (typeof fv.child != 'undefined') {
				// 		fv.child.map((sv, sk) => {
				// 			if (typeof sv.attr != 'undefined') {
				// 				if (typeof sv.attr.size == 'undefined') {
				// 					sv.attr.size = 'small'
				// 				}
				// 			}else{
				// 				sv.attr={size : 'small'}
				// 			}
				// 		})
				// 	}
				// })

				// console.log(json.child)
				this.compList = [...EButton.getJson()]
				if(typeof this.compList[0] !='undefined'&&typeof this.compList[0].attr!='undefined'&&typeof this.compList[0].attr.compId !='undefined'){
					this.currentId = this.compList[0].attr.compId
				}
				
				// let str = '{"node":"root","child":[{"node":"element","tag":"template","child":[{"node":"element","tag":"el-row","child":[{"node":"element","tag":"el-button","attr":{"class":"row","style":["margin:","10px;"],"type":"primary"},"child":[{"node":"text","text":"主要按钮"}]}]}]},{"node":"element","tag":"style","attr":{"scoped":""},"child":[{"node":"text","text":".row {color: red;}"}]}]}';
				// console.log(JSON.stringify(json2html(JSON.parse(str))))
				// for (let k in Element) {
				// 	if (typeof Element[k].name != 'undefined' && typeof Element[k].name == 'string') {
				// 		if (Element[k].name.indexOf("El") != -1) {
				// 			console.log(nanoid())
				// 			console.log(Element[k])
				// 			console.log('el-' + k.toLowerCase())
				// 		}
				// 	}
				// }
				// for (let k in Antd) {
				// if (typeof Antd[k].name != 'undefined' && typeof Antd[k].name == 'string') {
				// if (Antd[k].name.indexOf("El") != -1) {
				// console.log(Antd[k])
				// console.log('el-' + k.toLowerCase())
				// }
				// }
				// }
			},
			//一级分类点击
			tabtap(item) {
				this.currentId = item.attr.compId;
				this.scrollIntoView = 'main-' + item.compId;
			},
		}
	}
</script>

<style lang='scss'>
	page,
	.content {
		height: 85vh;
		display: flex;
	}

	.left-aside {
		flex-shrink: 0;
		width: 80px;
		height: 100%;
		position: relative;

		&:before {
			content: '';
			position: absolute;
			right: 0;
			top: 0;
			transform: translateY(-50%);
			height: 100vw;
			width: 1px;
			background-color: #ccc;
			border-radius: 0 4px 4px 0;
			opacity: .8;
		}

		.title {
			letter-spacing: 2px;
			font-size: 16px;
		}

		.sub-title {
			font-size: 12px;
		}
	}

	.f-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 100%;
		min-height: 55px;
		font-size: 28px;
		color: #333;
		position: relative;
		box-shadow: 0 1px 4px #ccc;

		&.active {
			color: $uni-theme-color;

			&:before {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36px;
				width: 4px;
				background-color: $uni-theme-color;
				border-radius: 0 2px 2px 0;
				opacity: .8;
			}
		}
	}

	.right-aside {
		flex: 1;
		overflow: hidden;
	}

	.s-item {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 35px;
		padding-top: 8px;
		font-size: 15px;
		color: #555;
	}

	.t-list {
		display: flex;
		justify-content: space-around;
		flex-wrap: wrap;
		width: 100%;
		background: #eee;
        padding:15px 10px;
		&:after {
			content: '';
			flex: 99;
			height: 0;
		}
	}

	.t-item {
		margin-bottom: 20px;
	}
</style>
